const fs = require('fs');

// for hikvision
const fz = require("./hik");

// 用户名 密码 IP 端口号
var userName = 'admin';
var password = 'cict12346';
var deviceAddress = '192.168.1.64';
var wPort = 8000;

function save_image(buf, length) {
    var base64Img = buf.toString('base64', 0, length); // base图片编码字符串
    var decodeImg = Buffer.from(base64Img, "base64");
    console.log("base64Img",base64Img);
    // 生成图片
    fs.writeFile('./img.jpg', decodeImg, function (err) {
        if (err) console.log(err);
    })
}

var start_hrTime = process.hrtime();
var loginResult = fz.Camera_Open(userName, password, deviceAddress, wPort);
// console.log("loginResult: ", loginResult);
var stop_hrTime = process.hrtime();
// console.log("login(ms):", (stop_hrTime[0] - start_hrTime[0])*1e3 + (stop_hrTime[1] - start_hrTime[1]) * 1e-6);

var sJpegPicBuffer = Buffer.alloc(1024*1024);
start_hrTime = process.hrtime();
var length = fz.Camera_ImgCap(loginResult, sJpegPicBuffer);
stop_hrTime = process.hrtime();
// console.log("image size:", length);
// console.log("buffer(ms):", (stop_hrTime[0] - start_hrTime[0])*1e3 + (stop_hrTime[1] - start_hrTime[1]) * 1e-6);
start_hrTime = process.hrtime();
save_image(sJpegPicBuffer, length);
stop_hrTime = process.hrtime();
// console.log("save image(ms):", (stop_hrTime[0] - start_hrTime[0])*1e3 + (stop_hrTime[1] - start_hrTime[1]) * 1e-6);

var CloseResult = fz.Camera_Close();
// console.log("CloseResult: ", CloseResult);

